/*
 * EmailBean.java
 *
 * Created on 6 Ноябрь 2006 г., 2:03
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package emailer;

import java.util.Properties;
import java.io.PrintWriter;
import java.io.IOException;

import javax.mail.*;
import javax.mail.internet.*;
/**
 *
 * @author alexog
 */
public class EmailBean {
    
    private static String DEFAULT_FROM = "gameadmin";
    
    /** Creates a new instance of EmailBean */
    public EmailBean() {
    }
    //Message fields
    private String username;
    private String password;
    protected String smtpHost;
    protected String to;
    protected String from;
    protected String content;
    protected String subject;
    //
    public void sendMessage() throws Exception{
        Properties prop = System.getProperties();
        //fil prop
        prop.put("mail.smtp.host",smtpHost);
        prop.put("mail.transport.protocol","smtp");
        prop.put("mail.smtp.auth","true");
        prop.put("mail.user","alexog");
        prop.put("mail.password","tolstyak");
        Session session = Session.getDefaultInstance(prop);
        Message mailMsg = new MimeMessage(session);//new email
        
        InternetAddress[] addresses = null;
        
        try{
            if (to!=null){
                //throws AddressException if not valid adress TODO
                
                addresses = InternetAddress.parse(to,false);
                mailMsg.setRecipients(Message.RecipientType.TO,addresses);
            }else {
                throw new MessagingException("The mail message req TO");
               }
            
            if (from!=null){
                mailMsg.setFrom(new InternetAddress(from));
            }else {
                throw new MessagingException("The mail message req FROM");
               }
            
            if (subject !=null)
                mailMsg.setSubject(subject);
            if (content !=null)
                mailMsg.setText(content);
            mailMsg.saveChanges();
            //send a Message
            Transport tr = session.getTransport("smtp");
            tr.connect(smtpHost,username,password);
            tr.sendMessage(mailMsg,mailMsg.getAllRecipients());
            tr.close();
        }catch(Exception ex){
            throw ex;
        }
     }//send Messsage

    public void setSmtpHost(String smtpHost) {
        this.smtpHost = smtpHost;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }
    
    private boolean check(String value){
        if(value==null||value.equals(""))
            return false;
            return true;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}
